//
//  VisitorView.swift
//  Practice
//
//  Created by miaozhihui on 2016/12/5.
//  Copyright © 2016年 DeKuTree. All rights reserved.
//

import UIKit
import SnapKit

/// 访客视图 - 处理用户未登录的界面显示
class VisitorView: UIView {
    /// imageName: 图片名称，首页设置为""
    /// title: 消息文字
    var visitorInfo:[String:String]? {
        didSet {
            guard let imgName = visitorInfo?["imageName"],
                let title = visitorInfo?["title"] else {
                return
            }
            messageLabel.text = title
            if imgName == "" {
                // 播放动画
                startAnimation()
                return
            }
            iconView.image = UIImage(named: imgName)
            // 隐藏小房子
            homeIconView.isHidden = true
            // 隐藏遮罩图片
            maskIconView.isHidden = true
        }
    }
    
    /// 开启首页转轮动画
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2*M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        anim.isRemovedOnCompletion = false
        iconView.layer.add(anim, forKey: nil)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setupUI()
    }
    
    /// 图标
    fileprivate lazy var iconView:UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    
    /// 遮罩图片
    fileprivate lazy var maskIconView:UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    
    /// 小房子
    fileprivate lazy var homeIconView:UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    
    /// 提示信息
    fileprivate lazy var messageLabel:UILabel = UILabel(title: "")

    /// 注册按钮
    lazy var registerButton:UIButton = UIButton(title: "注册", color: UIColor.orange, imageName: "common_button_white_disable")
    
    /// 登录按钮
    lazy var loginButton:UIButton = UIButton(title: "登录", color: UIColor.darkGray, imageName: "common_button_white_disable")
}

// MARK: - 设置界面
extension VisitorView {
    
    /// 设置界面
    fileprivate func setupUI() {
        // 添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 设置自动布局
        // 图标
        iconView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-60)
        }
        // 小房子
        homeIconView.snp.makeConstraints { (make) in
            make.center.equalTo(iconView)
        }
        // 消息文字
        messageLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp.bottom).offset(16)
            make.width.equalTo(230)
            make.height.equalTo(36)
        }
        // 注册按钮
        registerButton.snp.makeConstraints { (make) in
            make.left.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp.bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
        // 登录按钮
        loginButton.snp.makeConstraints { (make) in
            make.right.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.width.equalTo(registerButton)
            make.height.equalTo(registerButton)
        }
        // 遮罩图片
        maskIconView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.bottom.equalTo(registerButton.snp.bottom)
        }
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
    }
    
    // MARK: - 系统约束写法
    private func nativeConstraint() {
        // translatesAutoresizingMaskIntoConstraints 默认是 true 支持使用 setFrame 的方式设置控件位置
        // false 支持使用自动布局来设置控件位置
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        // 图标
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: -60))
        // 小房子
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .centerX, relatedBy: .equal, toItem: iconView, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .centerY, relatedBy: .equal, toItem: iconView, attribute: .centerY, multiplier: 1.0, constant: 0))
        // 消息文字
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .centerX, relatedBy: .equal, toItem: iconView, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .top, relatedBy: .equal, toItem: iconView, attribute: .bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 230))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 36))
        // 注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .left, relatedBy: .equal, toItem: messageLabel, attribute: .left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .top, relatedBy: .equal, toItem: messageLabel, attribute: .bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 36))
        // 登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .right, relatedBy: .equal, toItem: messageLabel, attribute: .right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .top, relatedBy: .equal, toItem: messageLabel, attribute: .bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 36))
        // 遮罩图片
        // VFL:可视化格式语言
        // H:水平方向
        // V:垂直方向
        // |:边界
        // []:包装控件
        // views:是一个字典[名字：控件名]
        // metrics:是一个字典[名字：NSNumber] 字符串中标识某一个数值
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask":maskIconView]))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[mask]-btnHeight-[regButton]", options: [], metrics: ["btnHeight":-36], views: ["mask":maskIconView,"regButton":registerButton]))
    }
}
